package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.CommunityDao;
import com.xueyu.dao.HouseDao;
import com.xueyu.dao.TermDao;
import com.xueyu.domain.HouseBean;
import com.xueyu.domain.TermBean;
import com.xueyu.domain.bed.Community;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/house")
public class HouseController {
    @Autowired
    CommunityDao communityDao;

    @Autowired
    TermDao termDao;

    @Autowired
    HouseDao houseDao;

    Map<String,Object> map = new HashMap<>();

    @RequestMapping("/init")
    public Map<String,Object> init(HttpServletRequest req){
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int curPage = Integer.parseInt(req.getParameter("curPage"));
        String search = req.getParameter("search");
        QueryWrapper<HouseBean> queryWrapper = new QueryWrapper<HouseBean>();
        Page<HouseBean> page = new Page<HouseBean>(curPage,pageSize);
        queryWrapper.like("houseName",search)
                .or().like("contact",search)
                .or().like("tel",search)
                .or().like("remark",search)
                .or().like("c.communityName",search);
        List<HouseBean> list = houseDao.selectLeftJoin(queryWrapper,page);
        map.put("list",list);
        map.put("code",200);
        map.put("total",page.getTotal());
        return map;
    }

    @RequestMapping("/insert")
    public Map insert(HouseBean houseBean){
        System.out.println("===="+houseBean);
        map.put("repeat", repeat(houseBean) || houseDao.insert(houseBean) == 0);
        return map;
    }

    @RequestMapping("/update")
    public Map update(HouseBean houseBean){
        map.put("changed",(houseDao.updateById(houseBean) > 0));
        return map;
    }

    @RequestMapping("/del/{id}")
    public Map del(@PathVariable("id") int id){
        map.put("delChanged",(houseDao.deleteById(id) > 0));
        return map;
    }

//    查找关联表（community）信息
    @RequestMapping("/search")
    public Map search(HttpServletRequest req){
        String search = req.getParameter("search");
        Page<Community> page = new Page<>(1,100);
        LambdaQueryWrapper<Community> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Community::getCommunityname,search);
        map.put("list",communityDao.selectPage(page, (search == null || search.equals("")) ? null : lambdaQueryWrapper).getRecords());
        map.put("result",page.getTotal() > 0);
        return map;
    }

    @RequestMapping("/search1")
    public Map search1(HttpServletRequest req){
        System.out.println("id===="+req.getParameter("id"));
        int id = Integer.parseInt(req.getParameter("id"));
        Page<TermBean> page = new Page<>(1,100);
        LambdaQueryWrapper<TermBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TermBean::getCommunityId,id);
        map.put("terms",termDao.selectPage(page,lambdaQueryWrapper).getRecords());
        return map;
    }

//    查找重复记录
    private boolean repeat(HouseBean houseBean){
        Map<String,Object> map1 = new HashMap<>();
        QueryWrapper<HouseBean> queryWrapper = new QueryWrapper<>();
        map1.put("communityId",houseBean.getCommunityId());
        map1.put("termId",houseBean.getTermId());
        map1.put("houseName",houseBean.getHouseName());
        queryWrapper.allEq(map1);
        return houseDao.selectOne(queryWrapper) != null;
    }
}
